AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")

include("shared.lua")
include("weld.lua")

module("Block", package.seeall )

local WeldSound = Sound("ambient/energy/electric_loop.wav")
local ReapirSound = Sound( "weapons/gauss/chargeloop.wav" )

function ResetAllHealth( state )

	local RemoveBad = RemoveProhibited()
	
	for _, block in ipairs( GetBlocks() ) do
	
		block:ResetLife()
		
		if state == Game.FIGHT then
			if CanFreeze( block, nil ) then
				block:SetAllowMotion( false )
			elseif RemoveBad then
				block:Remove()
			end
		end
		
	end
	
end

hook.Add("StateChange", "ResetBlockHealth", ResetAllHealth )

hook.Add("EntityTakeDamage", "MakeBlockDamange", function( ent, inflictor, attacker, amount, dmginfo )
	
	if ValidBlock( ent ) then		
		
		if Game.State() == Game.FIGHT then
		
			if ValidPlayer( attacker ) then
			
				if ent:Team() == attacker:Team() && !AllowTeamDamange() then
					return
				end
			
			end
			
			if DEBUG then
				print("Doing damage", ent, amount, dmginfo:GetDamage() )
			end
			
			ent:DoDamange( amount )
			
		end
		
		dmginfo:ScaleDamage( 0.0 ) //Game has it's own health system
	
	end	

end )

hook.Add("AllowSpawnBlock", "CheckTeamBlockLimit", function( ply, spawner )

	local Max = MaxLimit()
	
	if Max <= 0 then
		return
	end
	
	local Count = BlockCount( ply:Team() )
	
	if Count >= Max then
		return false
	end	

end )


hook.Add("GravGunPickupAllowed", "DissalowFight", function( ply, ent )
	
	if Game.State() == Game.FIGHT && ent.IsBlock then
		
		if ent:AllowMotion() == false then
			return false
		end
		
	end

end )

hook.Add("GravGunOnPickedUp", "ChangeSkin", function( ply, ent )
	
	if ent.IsBlock then
		ent:UpdateSkin( ply )
		
		ply._PickedBlock = ent
	end
	
end )

hook.Add("GravGunOnDropped", "RemovePicedBlock", function( ply, ent )
	
	if ent.IsBlock then
		ply._PickedBlock = nil
	end
	
end )

function CanFreeze( block, ply )
	return hook.Call("BlockFreeze", GAMEMODE, block, ply ) != false
end

function CheckMotion( ply, block, motion )
	
	if motion == true then
		block:SetAllowMotion( true )
		return
	end
	
	if !CanFreeze( block, ply ) then
		ply:PrintMessage(HUD_PRINTCENTER , "You are not allowed to freeze it there.")
		return
	end
	
	block:SetAllowMotion( false )

end

hook.Add("KeyRelease", "CheckPickedBlock", function( ply, key )
	
	if Game.State() == Game.BUILD then
		
		if key == IN_RELOAD then
			
			if ValidEntity( ply._PickedBlock ) then
				ply:ConCommand("-attack")	
				CheckMotion( ply, ply._PickedBlock, false )
			else
				
				local Block = AimOnBlock( ply )
				
				if Block then
					CheckMotion( ply, Block, !Block:AllowMotion() )
				end
			
			end
		
		end
		
	end
		
end )

hook.Add("KeyPress", "CheckUnweldBlock", function( ply, key )
	
	if Game.State() == Game.FIGHT then
	
		if key == IN_RELOAD and ActiveGun( ply ) then
			
			local Trace = ply:GetEyeTrace() 
			local block = Trace.Entity
	
			if ValidBlock( block ) && InRange( ply, Trace.HitPos ) then
				local Snd = CreateSound( ply, WeldSound )
				local StartState = block:AllowMotion()
				
				ply._WeldingBlock = {
					Block = block,
					StartTime = CurTime(),
					StartState = StartState,
					NextSpark = 0,
					Sound = Snd
				}
				
				Snd:Play()
				block:SetAllowMotion( false )
				StopRepair( ply )
				
				//print("Repair time: ", GetWeldTime( block, StartState ) )
				
				umsg.Start("Block", ply )
					umsg.Char( 0 )
					umsg.Entity( block )
					umsg.Long( CurTime() )
					umsg.Bool( StartState )				
				umsg.End()
				
			end
		
		end
		
	end

end )

function RepairTick( Ent )
	local NewHealth = math.min( Ent:Life() + RepairLife(), MaxHealth() )
	
	Ent:SetLife( NewHealth )
end

function PlayerReapirTick( ply )
	
	local Block = AimOnBlock( ply )
	
	if DEBUG then
		print("Repair tick: ", ply, Block )
	end
	
	if Block && Block:Team() == ply:Team() then
		
		RepairTick( Block )
		
		if !ply._PlayRepairSound then
			ply._PlayRepairSound = CreateSound( ply, ReapirSound )
			ply._PlayRepairSound:Play()
		end
		
	end	
	
end

function StopRepair( ply )
	if ply._PlayRepairSound then
		ply._PlayRepairSound:Stop()
		ply._PlayRepairSound = nil
	end
end

hook.Add("Think", "RepairBlock", function()
	if Game.State() == Game.FIGHT then
		
		for _, ply in ipairs( player.GetAll() ) do
			
			if ply._WeldingBlock then 
				if WeldThink( ply, ply._WeldingBlock ) == false then
					StopWeld( ply )
				end
				return
			end
			
			if ply:KeyDown( IN_USE ) and ActiveGun( ply ) then
				PlayerReapirTick( ply )
			else
				StopRepair( ply )
			end
			
		end
	
	end
end )

hook.Add("StateChange", "StopAllSound", function()
	for _, ply in pairs( player.GetAll() ) do
		
		StopWeld( ply )
		
		if ply._PlayRepairSound then
			ply._WeldingBlock.Sound:Stop()
			ply._WeldingBlock = nil	
		end
		
	end
end )